package com.julian.warehouse.activity;

import android.Manifest;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.julian.warehouse.R;
import com.julian.warehouse.base.SPBaseActivity;
import com.julian.warehouse.database.BackupTask;
import com.julian.warehouse.database.MyDBOpenHelper;
import com.julian.warehouse.dialog.MyAlertDialog;
import com.julian.warehouse.interfaces.OnBooleanListener;
import com.julian.warehouse.interfaces.OnResultListener;

import java.security.Permission;

//将数据库备份到sd卡
public class BackupActivity extends SPBaseActivity implements View.OnClickListener{

    private SQLiteDatabase db;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_backup);
        super.init();
        setHeadTitle(true,"数据库备份");


        requestPermission(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, new OnBooleanListener() {
            @Override
            public void onClick(boolean isAllHave, int[] position) {
                if (!isAllHave){
                    showMyDialog(false, "权限不足，请手动去设置中开启应用权限，系统将关闭", "退出", new MyAlertDialog.OnMyClickListener() {
                        @Override
                        public void onClick(MyAlertDialog dialog) {
                            finish();
                        }
                    });
                }
            }
        });



    }

    @Override
    public void initView() {
        findViewById(R.id.btn_backup).setOnClickListener(this);
        findViewById(R.id.btn_recover).setOnClickListener(this);

    }

    @Override
    public void initData() {
        MyDBOpenHelper myDBOpenHelper = new MyDBOpenHelper(this);
        db = myDBOpenHelper.getReadableDatabase();
    }

    @Override
    public void initEvent() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_backup:
                showMyDialog(true, "是否备份数据库到sd卡下的warehouseBackup文件夹中？", "确定", new MyAlertDialog.OnMyClickListener() {
                    @Override
                    public void onClick(MyAlertDialog dialog) {
                        dialog.dismiss();
                        BackupTask backuptask = new BackupTask(BackupActivity.this);
                        backuptask.setOnResultListener(new OnResultListener() {
                            @Override
                            public void onResult(int result) {
                                if(result!=-1){
                                    showToast("备份成功！");
                                }else {
                                    showToast("备份失败！");
                                }
                            }
                        });
                        backuptask.execute("backupDatabase");
                    }
                }, "取消", new MyAlertDialog.OnMyClickListener() {
                    @Override
                    public void onClick(MyAlertDialog dialog) {
                        dialog.dismiss();
                    }
                });
                break;
            case R.id.btn_recover:
                showMyDialog(true, "是否还原到上次备份的数据库？", "确定", new MyAlertDialog.OnMyClickListener() {
                    @Override
                    public void onClick(MyAlertDialog dialog) {
                        dialog.dismiss();
                        BackupTask backuptask = new BackupTask(BackupActivity.this);
                        backuptask.setOnResultListener(new OnResultListener() {
                            @Override
                            public void onResult(int result) {
                                if (result != -1) {
                                    showToast("还原成功！");
                                } else {
                                    showToast("还原失败！");
                                }
                            }
                        });
                        backuptask.execute("restroeDatabase");
                    }
                }, "取消", new MyAlertDialog.OnMyClickListener() {
                    @Override
                    public void onClick(MyAlertDialog dialog) {
                        dialog.dismiss();
                    }
                });
                break;
            default:break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        db.close();
    }
}
